﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GlxRRSReporter.Utils;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data;
using GlxRRSDB.Data;
using GlxRRSDB.Utils;


namespace GlxRRSReporter.DailyReporter
{
    public class DailyReporter
    {
        private const int NGUYEN_DU_CINEMA_ID = 1001;
        private const int NGUYEN_TRAI_CINEMA_ID = 1002;
        private const int TAN_BINH_CINEMA_ID = 1003;
        //
        private const int REPORT_SHEET_INDEX = 1;
        private const int DATA_SHEET_INDEX = 2;
        private static string TEMPLATE_DAILY_NAME = ".\\templates\\DailyReportTemplate.xls";
        private static string outName;
        private static string admissionFilmColumn = "C";
        private static string boFilmColumn = "C";
        private static int startAdmissionFilm = 34;
        private static int startBOFilm = 53;
        private static int totalFilm = 12;

        public static void init(string template, string outNameVal)
        {
            TEMPLATE_DAILY_NAME = template;
            outName = outNameVal;
        }
        public static void generateDailyReport(DateTime current,
            Dictionary<int, DailyReportData> dictAdmission,
            Dictionary<int, DailyReportData> dictBO,
            Dictionary<int, DailyReportData> dictCO,
            Dictionary<int, DailyReportData> dictOccupancy,
            Dictionary<int, Dictionary<DateTime, DailyReportData>> dictDetailAdmission,
            Dictionary<int, Dictionary<DateTime, DailyReportData>> dictDetailBO,
            Dictionary<int, Dictionary<DateTime, DailyReportData>> dictDetailCO,
            Dictionary<int, Dictionary<DateTime, DailyReportData>> dictDetailAdmissionFilm,
            Dictionary<int, Dictionary<DateTime, DailyReportData>> dictDetailBOFilm)
        {
            // load excel file
            Excel.Workbook workbook = ExportToExcel.openExcelWorkbooks(TEMPLATE_DAILY_NAME);
            Excel.Worksheet sheet = workbook.Worksheets.get_Item(REPORT_SHEET_INDEX);
            #region SET CURRENT DATE
            sheet.get_Range("L2").Formula = current;//.ToShortDateString();
            sheet.get_Range("B6").Formula = current;//.ToShortDateString();
            sheet.get_Range("B18").Formula = current;//.ToShortDateString();
            sheet.get_Range("H6").Formula = current;//.ToShortDateString();
            sheet.get_Range("H18").Formula = current;//.ToShortDateString();
            #endregion

            #region ADMISSION REPORT DATA
            DailyReportData data = dictAdmission[NGUYEN_DU_CINEMA_ID];
            if (data != null)
            {
                sheet.get_Range("C6").Formula = data.currentDate;
                sheet.get_Range("C7").Formula = data.currentWeek;
                sheet.get_Range("C8").Formula = data.currentMonth;
                sheet.get_Range("C9").Formula = data.currentQuarter;
                sheet.get_Range("C10").Formula = data.currentYear;
            }
            //
            if (data != null)
            {
                data = dictAdmission[NGUYEN_TRAI_CINEMA_ID];
                sheet.get_Range("D6").Formula = data.currentDate;
                sheet.get_Range("D7").Formula = data.currentWeek;
                sheet.get_Range("D8").Formula = data.currentMonth;
                sheet.get_Range("D9").Formula = data.currentQuarter;
                sheet.get_Range("D10").Formula = data.currentYear;
            }
            //
            if (data != null)
            {
                data = dictAdmission[TAN_BINH_CINEMA_ID];
                sheet.get_Range("E6").Formula = data.currentDate;
                sheet.get_Range("E7").Formula = data.currentWeek;
                sheet.get_Range("E8").Formula = data.currentMonth;
                sheet.get_Range("E9").Formula = data.currentQuarter;
                sheet.get_Range("E10").Formula = data.currentYear;
            }
            #endregion
            #region OCCUPANCY REPORT DATA
            //DailyReportData occ = dictOccupancy[NGUYEN_DU_CINEMA_ID];
            DailyReportData occ = null;
            if (dictOccupancy.ContainsKey(NGUYEN_DU_CINEMA_ID))
            {
                occ = dictOccupancy[NGUYEN_DU_CINEMA_ID];
                sheet.get_Range("I6").Formula = occ.currentDate;
                sheet.get_Range("I7").Formula = occ.currentWeek;
                sheet.get_Range("I8").Formula = occ.currentMonth;
                sheet.get_Range("I9").Formula = occ.currentQuarter;
                sheet.get_Range("I10").Formula = occ.currentYear;
            }
            //
            if (dictOccupancy.ContainsKey(NGUYEN_TRAI_CINEMA_ID))
            {
                occ = dictOccupancy[NGUYEN_TRAI_CINEMA_ID];
                sheet.get_Range("J6").Formula = occ.currentDate;
                sheet.get_Range("J7").Formula = occ.currentWeek;
                sheet.get_Range("J8").Formula = occ.currentMonth;
                sheet.get_Range("J9").Formula = occ.currentQuarter;
                sheet.get_Range("J10").Formula = occ.currentYear;
            }
            //
            if (dictOccupancy.ContainsKey(TAN_BINH_CINEMA_ID))
            {
                occ = dictOccupancy[TAN_BINH_CINEMA_ID];
                sheet.get_Range("K6").Formula = occ.currentDate;
                sheet.get_Range("K7").Formula = occ.currentWeek;
                sheet.get_Range("K8").Formula = occ.currentMonth;
                sheet.get_Range("K9").Formula = occ.currentQuarter;
                sheet.get_Range("K10").Formula = occ.currentYear;
            }
            #endregion
            #region BO REPORT DATA
            DailyReportData bo = dictBO[NGUYEN_DU_CINEMA_ID];
            sheet.get_Range("C18").Formula = bo.currentDate;
            sheet.get_Range("C19").Formula = bo.currentWeek;
            sheet.get_Range("C20").Formula = bo.currentMonth;
            sheet.get_Range("C21").Formula = bo.currentQuarter;
            sheet.get_Range("C22").Formula = bo.currentYear;
            //
            bo = dictBO[NGUYEN_TRAI_CINEMA_ID];
            sheet.get_Range("D18").Formula = bo.currentDate;
            sheet.get_Range("D19").Formula = bo.currentWeek;
            sheet.get_Range("D20").Formula = bo.currentMonth;
            sheet.get_Range("D21").Formula = bo.currentQuarter;
            sheet.get_Range("D22").Formula = bo.currentYear;
            //
            bo = dictBO[TAN_BINH_CINEMA_ID];
            sheet.get_Range("E18").Formula = bo.currentDate;
            sheet.get_Range("E19").Formula = bo.currentWeek;
            sheet.get_Range("E20").Formula = bo.currentMonth;
            sheet.get_Range("E21").Formula = bo.currentQuarter;
            sheet.get_Range("E22").Formula = bo.currentYear;
            #endregion
            #region CO REPORT DATA
            DailyReportData co;
            if(dictCO.ContainsKey(NGUYEN_DU_CINEMA_ID)) {
            co = dictCO[NGUYEN_DU_CINEMA_ID];
            sheet.get_Range("I18").Formula = co.currentDate;
            sheet.get_Range("I19").Formula = co.currentWeek;
            sheet.get_Range("I20").Formula = co.currentMonth;
            sheet.get_Range("I21").Formula = co.currentQuarter;
            sheet.get_Range("I22").Formula = co.currentYear;
            }
            //
            if (dictCO.ContainsKey(NGUYEN_TRAI_CINEMA_ID))
            {
                co = dictCO[NGUYEN_TRAI_CINEMA_ID];
                sheet.get_Range("J18").Formula = co.currentDate;
                sheet.get_Range("J19").Formula = co.currentWeek;
                sheet.get_Range("J20").Formula = co.currentMonth;
                sheet.get_Range("J21").Formula = co.currentQuarter;
                sheet.get_Range("J22").Formula = co.currentYear;
            }
            //
            if (dictCO.ContainsKey(TAN_BINH_CINEMA_ID))
            {
                co = dictCO[TAN_BINH_CINEMA_ID];
                sheet.get_Range("K18").Formula = co.currentDate;
                sheet.get_Range("K19").Formula = co.currentWeek;
                sheet.get_Range("K20").Formula = co.currentMonth;
                sheet.get_Range("K21").Formula = co.currentQuarter;
                sheet.get_Range("K22").Formula = co.currentYear;
            }
            #endregion

            sheet = workbook.Worksheets.get_Item(DATA_SHEET_INDEX);
            // title
            sheet.get_Range("C5").Formula = current.StartOfWeek(DayOfWeek.Friday);//.Date.ToString();
            sheet.get_Range("C15").Formula = current.StartOfWeek(DayOfWeek.Friday);//.Date.ToString();
            sheet.get_Range("C25").Formula = current.StartOfWeek(DayOfWeek.Friday);//.Date.ToString();
            //
            //sheet.get_Range("C34").Formula = current.StartOfWeek(DayOfWeek.Friday);//.Date.ToString();
            //sheet.get_Range("C53").Formula = current.StartOfWeek(DayOfWeek.Friday);//.Date.ToString();
            string addrAdmmissionFilm = admissionFilmColumn + startAdmissionFilm.ToString();
            string addrBOFilm = boFilmColumn + startBOFilm.ToString();
            sheet.get_Range(addrAdmmissionFilm).Formula = current.StartOfWeek(DayOfWeek.Friday);//.Date.ToString();
            sheet.get_Range(addrBOFilm).Formula = current.StartOfWeek(DayOfWeek.Friday);//.Date.ToString();
            //
            string[] column = new string[] { "B", "C", "D", "E", "F", "G", "H", "I" };
            string[] rowAdmission = new string[] { "6", "7", "8" };
            string[] rowBO = new string[] { "16", "17", "18" };
            string[] rowCO = new string[] { "26", "27", "28" };
            string[] rowAdmissionFilm = new string[] { "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46" };
            string[] rowBOFilm = new string[] { "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65" };


            #region CHART ADMISSION DETAIL
            DateTime startOfWeek = current.StartOfWeek(DayOfWeek.Friday);
            int columnIdx = 0;
            int rowIdx = -1;
            foreach (Dictionary<DateTime, DailyReportData> dict in dictDetailAdmission.Values)
            {
                //columnIdx=0;
                rowIdx++;
                foreach (DailyReportData detail in dict.Values)
                {
                    System.TimeSpan diff1 = detail.FromDate.Date.Subtract(startOfWeek.Date);
                    columnIdx = diff1.Days + 1;
                    //columnIdx++;
                    //
                    string addr = column[columnIdx] + rowAdmission[rowIdx];
                    sheet.get_Range(addr).Formula = detail.currentDate;
                }
            }
            #endregion
            #region CHART BO DETAIL
            columnIdx = 0;
            rowIdx = -1;
            foreach (Dictionary<DateTime, DailyReportData> dict in dictDetailBO.Values)
            {
                columnIdx = 0;
                rowIdx++;
                foreach (DailyReportData detail in dict.Values)
                {
                    System.TimeSpan diff1 = detail.FromDate.Date.Subtract(startOfWeek.Date);
                    columnIdx = diff1.Days + 1;
                    //columnIdx++;
                    //
                    string addr = column[columnIdx] + rowBO[rowIdx];
                    sheet.get_Range(addr).Formula = detail.currentDate;
                }
            }
            #endregion
            #region CHART CO DETAIL
            rowIdx = -1;
            foreach (Dictionary<DateTime, DailyReportData> dict in dictDetailCO.Values)
            {
                columnIdx = 0;
                rowIdx++;
                foreach (DailyReportData detail in dict.Values)
                {
                    System.TimeSpan diff1 = detail.FromDate.Date.Subtract(startOfWeek.Date);
                    columnIdx = diff1.Days + 1;
                    //columnIdx++;
                    //
                    string addr = column[columnIdx] + rowCO[rowIdx];
                    sheet.get_Range(addr).Formula = detail.currentDate;
                }
            }
            #endregion
            #region CHART Film Admission DETAIL
            columnIdx = -1;
            rowIdx = -1;
            foreach (Dictionary<DateTime, DailyReportData> dict in dictDetailAdmissionFilm.Values)
            {
                columnIdx = 0;
                rowIdx++;
                foreach (DailyReportData detail in dict.Values)
                {
                    System.TimeSpan diff1 = detail.FromDate.Date.Subtract(startOfWeek.Date);
                    columnIdx = diff1.Days + 1;
                    //columnIdx++;
                    //
                    sheet.get_Range(column[0] + rowAdmissionFilm[rowIdx]).Formula = detail.FilmTitle;
                    string addr = column[columnIdx] + rowAdmissionFilm[rowIdx];
                    sheet.get_Range(addr).Formula = detail.currentDate;
                }
            }
            #endregion
            #region CHART Film BO DETAIL
            rowIdx = -1;
            foreach (Dictionary<DateTime, DailyReportData> dict in dictDetailBOFilm.Values)
            {
                rowIdx++;
                foreach (DailyReportData detail in dict.Values)
                {
                    System.TimeSpan diff1 = detail.FromDate.Date.Subtract(startOfWeek.Date);
                    columnIdx = diff1.Days + 1;
                    //
                    sheet.get_Range(column[0] + rowBOFilm[rowIdx]).Formula = detail.FilmTitle;
                    //
                    string addr = column[columnIdx] + rowBOFilm[rowIdx];
                    sheet.get_Range(addr).Formula = detail.currentDate;
                }
            }
            #endregion
            // gen file name
            //workbook.SaveAs(".\\templates\\export\\DailyReport.xls");
            workbook.SaveAs(outName);
        }
    }
}
